package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Msg;
import com.pojo.Order;
import com.pojo.OrderItem;
import com.service.OrderItemsService;
import com.service.OrderService;
import com.shiro.controller.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RequiresPermissions(value={"orderTable:*"})
@Controller
public class OrderController extends BaseController {
    @Autowired
    OrderService orderService;

    @Autowired
    OrderItemsService orderItemsService;


    //创建订单
    // 最初的代码
//    @RequestMapping(value="/order/create")
//    public Msg addOrder () {
//        Order order = new Order();
//        int oid = orderService.addOrder(order);
//        return Msg.success().add("oid", oid);
//    }

    //创建订单 v2.0
    @RequestMapping(value="/order/create", method = RequestMethod.POST)
    @ResponseBody
    public Msg addOrder (@RequestParam(value = "tId") Integer tId, @RequestParam(value = "dinerNum")Integer dinerNum) {
        System.out.println(tId+"------"+ dinerNum);
        int oid = orderService.addOrder(tId, dinerNum);
        System.out.println(oid+"oid");
        if(oid < 0){
            return Msg.fail().add("errorMessage","餐桌已满座位不足，请选择其他餐桌");
        }
        return Msg.success().add("oid", oid);
    }


//    public void addOrderItem (OrderItem orderItem) {
//        orderService.addOrderItem(orderItem);
//    }

    //正式下单
    //最初的代码
//    @RequestMapping(value="order", method=RequestMethod.POST)
//    public Msg setOrder (@RequestBody Order order) {
//        List<OrderItem> list = order.getOrderItems();
//        for (OrderItem orderItem : list) {
//            addOrderItem (orderItem);
//        }
//        orderService.updateOrder(order);
//        return Msg.success();
//    }

    //正式下单 v2.0， 按下结束点餐的时候触发
//    @RequestMapping(value="order", method=RequestMethod.POST)
//    public Msg setOrder (@RequestBody Order order) {
//        List<OrderItem> list = order.getOrderItems();
////        for (OrderItem orderItem : list) {
////            orderItemsService.insertOrderItems(orderItem);
////        }
//        orderItemsService.insertOrderItemsAll(list);
////        orderService.updateOrder(order);
//        return Msg.success();
//    }


    // 删除订单 v2.0 没有修改过
    @RequestMapping(value="/order/{oId}",method= RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteOrder (@PathVariable("oId") String oId) {
        Integer id = Integer.parseInt(oId);
        System.out.println(id+" 删除的订单id是这个数值");
        orderService.deleteOrder(id);
        System.out.println("订单删除完毕返回-----");

        return Msg.success().add("message","成功删除");
    }



    //所有的订单数据全部一起提交, 一起下单
    @RequestMapping(value = "/order/place", method = RequestMethod.POST)
    @ResponseBody
    public Msg placeOrder(@RequestBody Order order) {
        if(order.getOrderItems().size() != 0){
            //1. 生成订单 返回订单id
            int orderId = orderService.addOrder(order.gettId(), order.getDinerNum());

            order.setoId(orderId);

            List<OrderItem> orderItemList = order.getOrderItems();

            //计算小计
            orderItemsService.calAllSubTotalMoney(orderItemList);

            //批量插入到订单项数据库
            orderItemsService.insertOrderItemsAll(orderId, orderItemList);

            //计算总计并设置总计数量
            order.setoTotalNum(orderService.getTotalQuantity(orderId));
            order.setoTotalMoney(orderService.getTotalMoney(orderId));

            //更新订单数据库表
            orderService.updateOrder(order);

             Msg msg =  Msg.success().add("message", "成功下单");
             msg.add("oId", orderId);
             return msg;
        }else {
            return Msg.fail().add("message","请选择菜品下单!");
        }

    }



    // 挂单
    @RequestMapping(value = "/order/suspend", method = RequestMethod.POST)
    public Msg suspendOrder(@RequestBody Order order){
        if(order.getOrderItems().size() != 0){
            //1. 生成订单 返回订单id
            int orderId = orderService.addOrder(order.gettId(), order.getDinerNum());

            order.setoId(orderId);

            List<OrderItem> orderItemList = order.getOrderItems();

            //计算小计
            orderItemsService.calAllSubTotalMoney(orderItemList);

            //批量插入到订单项数据库
            orderItemsService.insertOrderItemsAll(orderId, orderItemList);

            //计算总计并设置总计数量
            order.setoTotalNum(orderService.getTotalQuantity(orderId));
            order.setoTotalMoney(orderService.getTotalMoney(orderId));

            //更新订单数据库表
            orderService.updateOrder(order);
            orderService.orderSuspend(orderId); //更新状态

            Msg msg =  Msg.success().add("message", "成功下单");
            msg.add("oId", orderId);
            return msg;
        }else {
            return Msg.fail().add("message","请选择菜品下单!");
        }
    }





    //多了这行以及下方的代码

    //更新订单
    @RequestMapping(value = "/order/update", method= RequestMethod.PUT)
    @ResponseBody
    public Msg updateOrder(@RequestBody Order order){
//        System.out.println("状态："+order.getoStatus()+" 餐桌： "+ order.getTable().gettName());
        System.out.println("就餐人数："+order.getDinerNum());

        orderService.updateOrder(order);
        return Msg.success();
    }

    //按照订单号查询订单
    @RequestMapping(value="/order/{oId}",method = RequestMethod.GET)
    public Msg getOrder (@PathVariable("oId") String oId) {
        Integer id = Integer.parseInt(oId);
        Order order = orderService.getOrder(id);
        if (order != null) {
            return Msg.success().add("order", order);
        }
        else
            return Msg.fail();
    }







    //查询订单
    @RequestMapping(value = "order/all", method = RequestMethod.GET)
    @ResponseBody
    public Msg getAllOrder(){
        List<Order> orderList = orderService.getAllOrder();
        return Msg.success().add("wOrder", orderList);
    }

    //分页获取订单信息,先不做订单查询,并非分页查询订单
    @RequestMapping(value = "order/page", method = RequestMethod.GET)
    @ResponseBody
    public Msg getOrderByPage(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
        //顺序不能乱，一定要先
        List<Order> orderList = orderService.getAllOrder();

        //        System.out.println("orderlist的长度为。。。。"+orderList.size()+" 长度");

        PageHelper.startPage(pn, 10);

        PageInfo page = new PageInfo(orderList, 10);

        //        if(orderList.size() != )
        Msg msg = Msg.success().add("pageInfo", page);
        return msg;
    }

    @RequestMapping(value = "order/withorderitem", method = RequestMethod.GET)
    @ResponseBody
    public Msg getOrderWithOiById(@RequestParam(value = "oid") Integer oid){
        if(oid!=null){
            Order order = orderService.selectOrderWithItems(oid);
            return Msg.success().add("order", order);
        }else {
            return Msg.fail().add("message","订单id不能为空");
        }
    }

}
